package frame;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

import algoritm.Algoritm;
import container.Stare;
import guiObject.Pahar1;
import guiObject.Pahar2;
import guiObject.Play;
import operator.G1;
import operator.G2;
import operator.Operator;
import operator.T12;
import operator.T21;
import operator.Umplere1;
import operator.Umplere2;


public class MyFrame extends JFrame implements ActionListener {
	
	// GUI
	
	private JPanel parentPanel;
	private JPanel upPanel;
	private JPanel downPanel;
	private Umplere1 u1;
	private Umplere2 u2;
	private G1 g1;
	private G2 g2;
	private T12 t12;
	private T21 t21;
	private Play play;
	private Pahar1 pahar1;
	private Pahar2 pahar2;
	private JComboBox<String> algList;
	
	public MyFrame(String title) {
		setTitle(title);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	
		setSize(480,370);
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation((int)dim.getWidth() / 2 - 240,(int)dim.getHeight() / 2 - 185);		
		
		parentPanel = new JPanel();
		parentPanel.setLayout(new GridLayout(2, 0, 0, 0));
		setContentPane(parentPanel);
		
		upPanel = new JPanel();
		upPanel.setLayout(new GridLayout(1, 3, 0, 0));
		upPanel.setBorder(new EmptyBorder(20,15,10,15));
		parentPanel.add(upPanel);
		
		pahar1 = new Pahar1();
		upPanel.add(pahar1);
		
		JPanel inCenPanel = new JPanel(new FlowLayout());		
		String[] algs = { "BSF","DSF","DSFLimited","DSFIterativ" };
		algList = new JComboBox<String>(algs);
		algList.setSelectedIndex(0);
		inCenPanel.add(algList);		
		upPanel.add(inCenPanel);
		
		pahar2 = new Pahar2();
		upPanel.add(pahar2);
		
		downPanel = new JPanel();
		downPanel.setLayout(new FlowLayout());
		downPanel.setBorder(new EmptyBorder(80,20,20,30));
		parentPanel.add(downPanel);
		
		u1 = new Umplere1("u1",upPanel);
		u1.addActionListener(this);
		downPanel.add(u1);
		
		u2 = new Umplere2("u2",upPanel);
		u2.addActionListener(this);
		downPanel.add(u2);
		
		g1 = new G1("g1",upPanel);
		g1.addActionListener(this);
		downPanel.add(g1);
		
		g2 = new G2("g2",upPanel);
		g2.addActionListener(this);
		downPanel.add(g2);
		
		t12 = new T12("t12",upPanel);
		t12.addActionListener(this);
		downPanel.add(t12);
		
		t21 = new T21("t21",upPanel);
		t21.addActionListener(this);
		downPanel.add(t21);
		
		play = new Play("play");
		play.addActionListener(this);
		downPanel.add(play);	
			
		Algoritm.adaugaOperatori(u1);
		Algoritm.adaugaOperatori(u2);
		Algoritm.adaugaOperatori(g1);
		Algoritm.adaugaOperatori(g2);
		Algoritm.adaugaOperatori(t12);
		Algoritm.adaugaOperatori(t21);
		
		Stare.setStareStatic(new Stare(0,0));
	}
	
	public void actionPerformed(ActionEvent ac) {
		try {
			if(ac.getSource() == play) {
				System.out.println("play:");
				play.resetPahare(g1,g2);
				String choice = (String)algList.getSelectedItem();
				if(choice.equals("BSF")) {
					play.playBSF();
				}	else if(choice.equals("DSF")) {
					play.playDSF();
				} else if(choice.equals("DSFLimited")) {
					play.playDSFLimited();						
				} else if(choice.equals("DSFIterativ")) {
					play.playDSFIterativ();
				}		
				System.out.println("end animation:");
			} else {
				Stare.printStareStatic();
				((Operator)(ac.getSource())).executa();	
				Stare.printStareStatic();
			}
		} catch (final Exception ex) {
			Thread t = new Thread(new Runnable() {
				public void run() {
					ErrorFrame errFrame = new ErrorFrame("Eroare",ex.getMessage());
					errFrame.setVisible(true);
				}
			});
			t.start();
		}		
	}
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
					MyFrame frame = new MyFrame("2PAHARE");
					frame.setVisible(true);
				} catch(Exception ex) {
					ex.printStackTrace();
				}
			}
		});	
		
	}

}
